package com.codedancer.campus.manager.api.dynamic;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codedancer.campus.common.anno.InterfacePermission;
import com.codedancer.campus.common.core.BaseApi;
import com.codedancer.campus.common.response.ApiResponse;
import com.codedancer.campus.manager.api.dynamic.ro.DynamicExamineRo;
import com.codedancer.campus.manager.api.dynamic.ro.DynamicPageSearchRo;
import com.codedancer.campus.manager.service.IDynamicService;
import com.codedancer.campus.manager.service.bo.dynamic.DynamicExamineBo;
import com.codedancer.campus.manager.service.bo.dynamic.DynamicPageSearchBo;
import com.codedancer.campus.manager.service.vo.dynamic.DynamicPageVo;
import com.codedancer.campus.manager.service.vo.dynamic.DynamicVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/dynamic")
@CrossOrigin(methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE,
    RequestMethod.PUT}, maxAge = 3600)
@Api(value = "动态Api", description = "动态Api", tags = "动态Api")
public class DynamicApi extends BaseApi {

    @Autowired
    private IDynamicService dynamicService;

    @GetMapping("/page")
    @ApiOperation("分页查询动态")
    @InterfacePermission(encodes = {"superAdmin","treeHoleAdmin"})
    public ApiResponse<IPage<DynamicPageVo>> page(@ApiParam(value = "动态分页查询Ro", required = false)
        DynamicPageSearchRo dynamicPageSearchRo) {
        DynamicPageSearchBo dynamicPageSearchBo = BeanUtil
            .copyProperties(dynamicPageSearchRo, DynamicPageSearchBo.class);
        return success(dynamicService.page(dynamicPageSearchBo));
    }

    @GetMapping("/detail/{uuid}")
    @ApiOperation("动态详情")
    @InterfacePermission(encodes = {"superAdmin","treeHoleAdmin"})
    public ApiResponse<DynamicVo> detail(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "动态uuid", required = true) String uuid) {
        return success(dynamicService.detail(uuid));
    }

    @PutMapping("/no-pass/{uuid}")
    @ApiOperation("审核未通过")
    @InterfacePermission(encodes = {"superAdmin","treeHoleAdmin"})
    public ApiResponse<Void> noPass(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "动态uuid", required = true) String uuid,
        @RequestBody @ApiParam(value = "动态审核Ro", required = true) DynamicExamineRo dynamicExamineRo) {
        DynamicExamineBo dynamicExamineBo = BeanUtil
            .copyProperties(dynamicExamineRo, DynamicExamineBo.class);
        dynamicExamineBo.setCurrentUserUuid(currentLoginUuid());
        dynamicExamineBo.setUuid(uuid);
        dynamicService.noPass(dynamicExamineBo);
        return success();
    }

    @PutMapping("/pass/{uuid}")
    @ApiOperation("审核通过")
    @InterfacePermission(encodes = {"superAdmin","treeHoleAdmin"})
    public ApiResponse<Void> pass(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "动态uuid", required = true) String uuid,
        @RequestBody @ApiParam(value = "动态审核Ro", required = true) DynamicExamineRo dynamicExamineRo) {
        DynamicExamineBo dynamicExamineBo = BeanUtil
            .copyProperties(dynamicExamineRo, DynamicExamineBo.class);
        dynamicExamineBo.setUuid(uuid);
        dynamicExamineBo.setCurrentUserUuid(currentLoginUuid());
        dynamicService.pass(dynamicExamineBo);
        return success();
    }

    @PutMapping("/open-comment/{uuid}")
    @ApiOperation("开启评论")
    @InterfacePermission(encodes = {"superAdmin","treeHoleAdmin"})
    public ApiResponse<Void> openComment(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "动态uuid", required = true) String uuid) {
        dynamicService.openComment(uuid);
        return success();
    }

    @PutMapping("/close-comment/{uuid}")
    @ApiOperation("关闭评论")
    @InterfacePermission(encodes = {"superAdmin","treeHoleAdmin"})
    public ApiResponse<Void> closeComment(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "动态uuid", required = true) String uuid) {
        dynamicService.closeComment(uuid);
        return success();
    }

    @DeleteMapping("/delete/{uuid}")
    @ApiOperation("删除动态")
    @InterfacePermission(encodes = {"superAdmin","treeHoleAdmin"})
    public ApiResponse<Void> delete(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "动态uuid", required = true) String uuid) {
        dynamicService.delete(uuid);
        return success();
    }

    @PostMapping("/export")
    @ApiOperation("导出动态")
    @InterfacePermission(encodes = {"superAdmin","treeHoleAdmin"})
    public void export(HttpServletRequest request, HttpServletResponse response,
        @ApiParam(value = "动态分页查询Ro", required = false)
            DynamicPageSearchRo dynamicPageSearchRo) {
        DynamicPageSearchBo dynamicPageSearchBo = BeanUtil
            .copyProperties(dynamicPageSearchRo, DynamicPageSearchBo.class);
        dynamicService.export(request, response, dynamicPageSearchBo);
    }

    @DeleteMapping("/batch-delete")
    @ApiOperation("批量删除")
    @InterfacePermission(encodes = {"superAdmin","treeHoleAdmin"})
    public ApiResponse<Void> batchDelete(@RequestBody List<String> uuids) {
        dynamicService.batchDelete(uuids);
        return success();
    }

    @GetMapping("/page-by-circle-uuid/{circleUuid}")
    @ApiOperation("分页查询某个圈子下的动态")
    @InterfacePermission(encodes = {"superAdmin","treeHoleAdmin"})
    public ApiResponse<IPage<DynamicPageVo>> pageByCircleUuid(
        @ApiParam(value = "动态分页查询参数", required = false)
            DynamicPageSearchRo dynamicPageSearchRo,
        @PathVariable(value = "circleUuid", required = true) @ApiParam(value = "圈子uuid", required = true) String circleUuid) {
        DynamicPageSearchBo dynamicPageSearchBo = BeanUtil
            .copyProperties(dynamicPageSearchRo, DynamicPageSearchBo.class);
        dynamicPageSearchBo.setCircleUuid(circleUuid);
        return success(dynamicService.pageByCircleUuid(dynamicPageSearchBo));
    }
}
